package com.lxgzhw.student_manager02.controller;

import com.lxgzhw.student_manager02.domain.Result;
import com.lxgzhw.student_manager02.domain.User;
import com.lxgzhw.student_manager02.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/listData")
    @ResponseBody
    public Result<User> listData(Integer page, Integer limit) {
        return userService.page(page, limit);
    }

    @PostMapping("/save")
    public String add(User user, String password1) {
        if (password1 != null && password1.equals(user.getPassword())) {
            userService.add(user);
            return "redirect:/";
        }
        return "redirect:/register";
    }

    @PostMapping("/login")
    public String login(User user) {
        if (userService.find(user)) {
            return "redirect:/";
        }
        ;
        return "redirect:/login";
    }

    @GetMapping("/list")
    public String list() {
        return "userList";
    }
}
